package com.scpii.universal.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.scpii.universal.hibernate.dao.BaseDao;
import com.scpii.universal.hibernate.entity.ContentTab;
import com.scpii.universal.hibernate.entity.DataSortTab;
import com.scpii.universal.hibernate.entity.SortColumnPOJO;
import com.scpii.universal.hibernate.entity.SortColumnsTab;
import com.scpii.universal.hibernate.entity.SortCondition;
import com.scpii.universal.hibernate.intface.BaseInterface;
import com.scpii.universal.util.Const;
import com.scpii.universal.util.SQLs;

public class ContentTabService implements BaseInterface<ContentTab> {
	private static final Logger log = LoggerFactory
			.getLogger(ContentTabService.class);
//	private BaseDao<ContentTab> contentTabDao = SpringBeanFactory.contentTabDao;
	private BaseDao<ContentTab> contentTabDao;
	private BaseDao<DataSortTab> dataSortDao;
	private BaseDao<SortColumnsTab> sortColumnsDao;

	public ContentTab get(Integer id) throws Exception {
		return contentTabDao.get(id);
	}

	public List<ContentTab> getAll() throws Exception {
		return contentTabDao.getAll();
	}

	public ContentTab get(String propertyName, Object value) throws Exception {
		return null;
	}

	public List<ContentTab> getList(String propertyName, Object value)
			throws Exception {
		return null;
	}

	public Long getTotalCount() throws Exception {
		return null;
	}

	public void save(ContentTab obj) throws Exception {
		contentTabDao.save(obj);
	}

	public void update(ContentTab obj) throws Exception {

	}

	public void delete(ContentTab obj) throws Exception {

	}

	public void delete(Integer id) throws Exception {

	}

	public List<ContentTab> hqlGetList(String hql, Map<String, Object> argsMap)
			throws Exception {
		return contentTabDao.hqlGetList(hql, argsMap);
	}
	
	public List<ContentTab> findByHql(String hql, Map<String, Object> argsMap)
			throws Exception {
		return contentTabDao.findByHql(hql, argsMap);
	}

	public void hqlUpdate(String namedHqlQuery, Map<String, Object> argsMap)
			throws Exception {
		contentTabDao.hqlUpdate(namedHqlQuery, argsMap);
	}

	public Long hqlGetNums(String namedHqlQuery, Map<String, Object> argsMap)
			throws Exception {
		return contentTabDao.hqlGetNums(namedHqlQuery, argsMap);
	}

	public List<ContentTab> sqlGetList(String sqls, Map<String, Object> argsMap)
			throws Exception {
		return contentTabDao.sqlGetList(sqls, argsMap);
	}

	public void sqlUpdate(String sqls, Map<String, Object> argsMap)
			throws Exception {
		contentTabDao.sqlUpdate(sqls, argsMap);
	}

	public Long sqlGetNum(String sqls, Map<String, Object> argsMap)
			throws Exception {
		return contentTabDao.sqlGetNum(sqls, argsMap);
	}
	
	/**
	 * 根据分类ID，获得与分类相关的内容列表
	 * @param catelogId
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-7-17上午01:37:44
	 */
	public List<ContentTab> getContentListByCatelogId(int catelogId,int appId)throws Exception{
		if (catelogId>0) {
			List<ContentTab> contentList = new ArrayList<ContentTab>();
			//配置传递参数
			Map<String, Object> argsMap = new HashMap<String, Object>();
			argsMap.put("catelogId", catelogId);
			argsMap.put("appId", appId);
			contentList =this.hqlGetList("GET_CONTENT_LIST_BY_CATELOGID", argsMap);
			return contentList;
		}
		return null;
	}
	
	/**
	 * 获得查询结果的页数
	 * @param catelogId
	 * @param appId
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-8-14下午10:58:53
	 */
	public Integer getContentListPageCount(int catelogId,int appId)throws Exception{
		if (catelogId>0) {
			Map<String, Object> argsMap = new HashMap<String, Object>();
			argsMap.put("catelogId", catelogId);
			argsMap.put("appId", appId);
			Long resultCount = this.hqlGetNums("GET_CONTENT_PAGENO_BY_CATELOGID", argsMap);
			int result = resultCount.intValue();
			int pageCount = contentTabDao.getRecordPageNum(result, Const.MOBILE_CLIENT_NO_PERPAGE);
			return pageCount;
		}
		return 0;
	}
	
	/**
	 * 分页查询内容列表
	 * @param catelogId
	 * @param appId
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-8-14下午11:00:48
	 */
	public List<ContentTab> getContentListByPage(int catelogId,int appId,int pageNum)throws Exception{
		if (catelogId>0) {
			List<ContentTab> contentList = new ArrayList<ContentTab>();
			int MAXROWNUM = Const.MOBILE_CLIENT_NO_PERPAGE * pageNum;
			int MINROWNUM = Const.MOBILE_CLIENT_NO_PERPAGE * (pageNum - 1);
			//配置传递参数
			Map<String, Object> argsMap = new HashMap<String, Object>();
			argsMap.put("cateId", catelogId);
			argsMap.put("appId", appId);
			argsMap.put("MAXROWNUM", MAXROWNUM);
			argsMap.put("MINROWNUM", MINROWNUM);
			
			contentList = this.sqlGetList(SQLs.GET_PAGE_VIEW_CONTENT_LIST_BY_PAGE, argsMap);
			return contentList;
		}
		return null;
	}
	
	/**
	 * 根据排序字段分页查询内容列表
	 * @author Taylor
	 * 2012-11-22 上午09:47:21
	 * @param catelogId
	 * @param appId
	 * @param pageNum
	 * @return
	 * @throws Exception
	 * List<ContentTab>
	 */
	public List<ContentTab> getContentListByPage(SortCondition condition,int appId)throws Exception{
		if (condition.getIntCateId()>0) {
			List<ContentTab> contentList = new ArrayList<ContentTab>();
			int MAXROWNUM = Const.MOBILE_CLIENT_NO_PERPAGE * (condition.getIntPageNum());
			int MINROWNUM = Const.MOBILE_CLIENT_NO_PERPAGE * (condition.getIntPageNum() - 1);
			//配置传递参数
			Map<String, Object> argsMap = new HashMap<String, Object>();
			argsMap.put("cateId", condition.getIntCateId());
			argsMap.put("appId", appId);
			argsMap.put("MAXROWNUM", MAXROWNUM);
			argsMap.put("MINROWNUM", MINROWNUM);
			
			StringBuffer sb = new StringBuffer(SQLs.PUBLIC_STRS_HEAD);
			sb.append(" (SELECT C.* FROM CONTENT_TAB C");
			sb.append(populateSortRule(condition));
			sb.append(") " + SQLs.PUBLIC_STRS_BACK);
			
			contentList = this.sqlGetList(sb.toString(), argsMap);
			return contentList;
		}
		return null;
	}
	
	/**
	 * 封装排序规则
	 * @author Taylor
	 * 2012-11-22 上午10:07:34
	 * @param condition
	 * String
	 */
	private String populateSortRule(SortCondition condition) {
		StringBuffer sb = new StringBuffer();
		if(condition.getSortColumn() != null) {
			//客户端请求包含了排序参数
			daymicGetSortSql(condition.getSortColumn(), sb);
		} else {
			//查询生成平台设定的排序规则
			List<DataSortTab> lstSelectedSortCols = getSelectedSortCols(condition.getIntViewId());
			if (lstSelectedSortCols != null && lstSelectedSortCols.size() > 0) {
				DataSortTab dataSortTab = lstSelectedSortCols.get(0);
				daymicGetSortSql(new SortColumnPOJO(dataSortTab.getIntSortColumnId(),
								dataSortTab.getIntSortRule(),dataSortTab.getIntIsAttr()), sb);
			} else {
				//default
				sb.append(" WHERE C.INT_DELETE_SIGN=0 AND C.INT_CATE_ID=:cateId AND C.INT_APP_ID=:appId");
				sb.append(" ORDER BY INT_SORT_ORDER DESC,DAT_UPDATE_DATETIME DESC,INT_CONTENT_ID DESC");
			}
		}
		return sb.toString();
	}

	/**
	 * 生成排序SQL语句
	 * @author Taylor
	 * 2012-11-22 下午02:28:02
	 * @param condition
	 * @param sb
	 * void
	 */
	private void daymicGetSortSql(SortColumnPOJO sortColumnPojo, StringBuffer sb) {
		if(sortColumnPojo.getIntIsAttr() == 0){
			//非属性字段排序
			sb.append(" WHERE C.INT_DELETE_SIGN=0 AND C.INT_CATE_ID=:cateId AND C.INT_APP_ID=:appId");
			SortColumnsTab column = sortColumnsDao.get(sortColumnPojo.getIntSortColumnId());
			sb.append(" ORDER BY C.INT_SORT_ORDER DESC");
			sb.append(",C." + column.getVarSortColumnName() + " ");
		} else {
			sb.append(" LEFT JOIN ATTRIBUTE_VALUE_TAB A ON C.INT_CONTENT_ID=A.INT_CONTENT_ID");
			sb.append(" WHERE C.INT_DELETE_SIGN=0 AND C.INT_CATE_ID=:cateId AND C.INT_APP_ID=:appId");
			sb.append(" AND A.INT_ATTRI_ID=" + sortColumnPojo.getIntSortColumnId());
			sb.append(" ORDER BY C.INT_SORT_ORDER DESC");
			sb.append(",TO_CHAR(A.VAR_ATTRI_VALUE) ");
		}
		sb.append(sortColumnPojo.getIntSortRule() == 0 ? "ASC" : "DESC");
		sb.append(",C.INT_CONTENT_ID DESC");
	}
	
	/**
	 * 获取排序字段
	 * @author Taylor
	 * 2012-11-21 下午05:22:33
	 * @param intViewId
	 * @return
	 * List<DataSortTab>
	 */
	private List<DataSortTab> getSelectedSortCols(int intViewId) {
		Map<String, Object> argsMap = new HashMap<String, Object>();
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT * FROM DATA_SORT_TAB WHERE INT_DELETE_SIGN=0");
		sb.append(" AND INT_VIEW_ID=:intViewId ORDER BY INT_COLUMN_ORDER");
		argsMap.put("intViewId", intViewId);
		return dataSortDao.sqlGetList(sb.toString(), argsMap);
	}
	
	/**
	 * 分页查询
	 * @author Taylor
	 * 2012-9-18 下午02:11:40
	 * @param pageNo
	 * @param pageSize
	 * @param sql
	 * @param map
	 * @return
	 * @throws Exception
	 * List<ContentTab>
	 */
	public List<ContentTab> getContentByPageNo(final int pageNo, final int pageSize,
			final String sql, final Map<String,Object> map) throws Exception {
		return (List<ContentTab>) contentTabDao.findBySql(pageNo, pageSize, sql, map);
	}
	//Setter

	public void setContentTabDao(BaseDao<ContentTab> contentTabDao) {
		this.contentTabDao = contentTabDao;
	}

	public void setDataSortDao(BaseDao<DataSortTab> dataSortDao) {
		this.dataSortDao = dataSortDao;
	}

	public void setSortColumnsDao(BaseDao<SortColumnsTab> sortColumnsDao) {
		this.sortColumnsDao = sortColumnsDao;
	}
	
	
}